using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace CalcSharp.Core
{
    public enum CalculatorModes
    {
        Deg = 0,
        Rad = 1,
        Grad = 2,
        CPLX = 3,
    }

    public sealed class MiscCoreFuncts
    {
        private MiscCoreFuncts() { }
        public static bool protectedconstant(string s)
        {
            s = s.ToUpper();
            if (s == "$PI" || s == "$E") return true;
            else return false;
        }
        /// <summary>
        /// split a text by spaces, texts within a "" wil not be splited
        /// </summary>
        /// <param name="inp">the input string to be splitted</param>
        /// <returns>a string array of the substrings</returns>
        public static string[] split(string inp, bool incsep)
        {
            inp = " " + inp;
            string[] array = Regex.Split(inp, "\"([^\"]*)\"");
            List<string> temp = new List<string>();
            string[] spacediv;
            foreach (string s in array)
            {
                if (s.StartsWith(" "))
                {
                    spacediv = s.Split(' ');
                    for (int i = 0; i < spacediv.Length; i++)
                    {
                        if (spacediv[i].Length >= 1) temp.Add(spacediv[i]);
                    }
                }
                else if (!s.StartsWith(" ") && s.Length >= 1)
                {
                    if (incsep) temp.Add("\"" + s + "\"");
                    else temp.Add(s);
                }
            }
            return temp.ToArray();
        }
        /// <summary>
        /// split a string into x parts
        /// </summary>
        /// <param name="inp">the input string</param>
        /// <param name="len">the split length</param>
        /// <param name="reverse">reverse order</param>
        /// <returns>a string array of the substrings</returns>
        public static string[] split(string inp, int len)
        {
            List<string> tmp = new List<string>();
            if (len >= inp.Length) tmp.Add(inp);
            else
            {
                for (int i = (len-1); i <= inp.Length; i += len)
                {
                    tmp.Add(inp.Substring(i-1, len));
                }
            }
            return tmp.ToArray();
        }
        public static string FormatDouble(double input)
        {
            string gchar = CultureInfo.CurrentCulture.NumberFormat.CurrencyGroupSeparator;
            string fchar = CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            StringBuilder sb = new StringBuilder();
            bool passed = false;
            int j = 1;
            int i;
            char[] ar;
            string text = input.ToString();
            if (text.Contains(fchar))
            {
                for (i = text.Length - 1; i >= 0; i--)
                {
                    if (!passed && text[i] != fchar[0]) sb.Append(text[i]);
                    else if (text[i] == fchar[0])
                    {
                        sb.Append(text[i]);
                        passed = true;
                    }
                    if (passed && text[i] != fchar[0])
                    {
                        sb.Append(text[i]);
                        if (j % 3 == 0) sb.Append(gchar);
                        j++;
                    }
                }
                ar = sb.ToString().ToCharArray();
                Array.Reverse(ar);
                return new string(ar);
            }
            else
            {
                for (i = text.Length - 1; i >= 0; i--)
                {
                    sb.Append(text[i]);
                    if (j % 3 == 0) sb.Append(gchar);
                    j++;
                }
                ar = sb.ToString().ToCharArray();
                Array.Reverse(ar);
                return new string(ar);
            }
        }
        public static int CharCount(string input, char c)
        {
            int count = 0;
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == c) count++;
            }
            return count;
        }
        public static DialogResult ShowDialog(Form f)
        {
            f.FormBorderStyle = FormBorderStyle.FixedDialog;
            f.MinimizeBox = false;
            f.MaximizeBox = false;
            f.ShowInTaskbar = false;
            return f.ShowDialog();
        }
    }
}